package com.himilco.ocana.workflow.core.bo.sm;

import java.util.Date;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import com.himilco.ocana.workflow.core.dao.entities.Ticket;

public class AutomationInProgressClient {
	public static final void main(String[] args) {
		try {
			StatefulKnowledgeSession ksession = createKnowledgeSession();
			KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
			Ticket ticket = new Ticket();
			ticket.setStatus("AUTOMATION_IN_PRPOGRESS");
			ticket.setAutomaticDispachingMode(true);
			ticket.setContent("testing content");
			ticket.setStartTime(new Date());
			ticket.setIdTicket(1);
			ticket.setReparingSwitchTurnedOn(true);
			ksession.insert(ticket);
			ksession.fireAllRules();
			// sendNotifMail
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static StatefulKnowledgeSession createKnowledgeSession() {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(
				ResourceFactory.newClassPathResource("AutomationInProgress.drl"),
				ResourceType.DRL);

		if (kbuilder.hasErrors()) {
			if (kbuilder.getErrors().size() > 0) {
				for (KnowledgeBuilderError kerror : kbuilder.getErrors()) {
					System.err.println(kerror);
				}
			}
		}

		KnowledgeBase kbase = kbuilder.newKnowledgeBase();
		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
		return ksession;
	}
}
